﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Table = FCNS.Data.Table;

namespace StoreS.UC
{
    /// <summary>
    /// UCstore.xaml 的交互逻辑
    /// </summary>
    public partial class UCStoreS : UserControl
    {
        public UCStoreS()
        {
            InitializeComponent();

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                InitVar();
                InitEvent();
            }

            InitUI();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            InitUI();
        }

        public delegate void Selected(Table.StoreS obj);
        public event Selected SelectedObjectEvent;

        public Table.StoreS SelectedObject
        {
            get
            {
                if (comboBoxStore.SelectedItem == null)
                    return null;
                else
                    return (Table.StoreS)comboBoxStore.SelectedItem;
            }
            set { comboBoxStore.SelectedItem = value; }
        }

        public Int64 SelectedObjectId
        {
            get { return SelectedObject == null ? -1 : SelectedObject.Id; }
            set { comboBoxStore.SelectedValue = value; }
        }
        /// <summary>
        /// 仓库的收发货地址,这个是可以编辑的,因为有可能更仓库的默认地址不一样啊
        /// </summary>
        public string Address
        {
            get { return textBoxAddress.Text; }
            set { textBoxAddress.Text = value; }
        }

        bool showAddress = true;
        public bool ShowAddress
        {
            get { return showAddress; }
            set
            {
                showAddress = value;
                labelAddress.Visibility = value ? Visibility.Visible : Visibility.Hidden;
                textBoxAddress.Visibility = value ? Visibility.Visible : Visibility.Hidden;
                comboBoxStore.Width = value ? 200 : this.ActualWidth - comboBoxStore.Margin.Left;
            }
        }

        double marginLeft = 200;
        /// <summary>
        /// 地址的左边距
        /// </summary>
        public double AddressMarginLeft
        {
            get { return marginLeft; }
            set
            {
                marginLeft = value;
                InitUI();
            }
        }

        public string NameTitle
        {
            get { return labelName.Content.ToString(); }
            set
            {
                labelName.Content = value;
                InitUI();
            }
        }

        public string AddressTitle
        {
            get { return labelAddress.Content.ToString(); }
            set
            {
                labelAddress.Content = value;
                InitUI();
            }
        }

        private void InitUI()
        {
            comboBoxStore.Margin = new Thickness(labelName.ActualWidth, 0, 0, 0);
            comboBoxStore.Width = showAddress ? 200 : this.ActualWidth - comboBoxStore.Margin.Left;

            labelAddress.Margin = new Thickness(marginLeft, 0, 0, 0);
            textBoxAddress.Margin = new Thickness(marginLeft + labelAddress.ActualWidth, 0, 0, 0);
        }


        private void InitVar()
        {
            comboBoxStore.DisplayMemberPath = "Name";
            comboBoxStore.SelectedValuePath = "Id";
            comboBoxStore.ItemsSource = Table.StoreS.GetList(Table.StoreS.EnumEnable.启用);
            if (comboBoxStore.HasItems)
                comboBoxStore.SelectedIndex = 0;
        }

        private void InitEvent()
        {
            comboBoxStore.SelectionChanged += (ss, ee) =>
            {
                if (comboBoxStore.SelectedItem != null)
                    textBoxAddress.Text = ((Table.StoreS)comboBoxStore.SelectedItem).Address;

                if (SelectedObjectEvent != null && comboBoxStore.SelectedItem != null)
                    SelectedObjectEvent((Table.StoreS)comboBoxStore.SelectedItem);
            };
        }
    }
}
